import React, { useMemo } from 'react'
import { AccessProvider, modifyRoutesByAccess, modifyRoutesByAccessMenus, getAccessMenusPaths } from './Access';
import { useModel } from '@sui/sui';

export default function Provider(props){
    const { children, routes, accessStrictMode, getAccess } = props;
    const { context = {} } = useModel("context") || {};
    // 初始化access
    const { access, accessMenus } = useMemo(()=>({
        access: getAccess ? getAccess({}, context) : context.access,
        accessMenus: context.accessMenus
    }), [ context ])

    if( accessStrictMode || access ){ // 非严格模式， 只有设置了access状态，才进行权限校验
        modifyRoutesByAccess(routes, access, accessStrictMode === true, accessStrictMode === true); // 严格模式下，默认父级unaccessiable
    }
    if( accessMenus ){
        const accessPaths = getAccessMenusPaths(accessMenus)
        modifyRoutesByAccessMenus(routes, accessPaths, accessStrictMode === true, accessStrictMode === true)
    }
    return <AccessProvider access={access}>
        {children}
    </AccessProvider>
};

Provider.displayName = 'AccessRoutesProvider';
  